import { apiGetHelpComplete, apiGetHelpUserOrderList, apiGetHelpCommission, apiOrderTakeDelivery } from '../../api/index'
import { formatTime } from '../../common/js/timer'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    idx: 1,
    height: 0,
    loads: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX
    })
    this.getHelpList()
  },
  // 导航点击事件
  helpComm (e) {
    this.setData({
      idx: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index === 1) {
      this.getHelpList()
    } else if (e.currentTarget.dataset.index === 2) {
      this.getHelpOrder()
    } else {
      this.getHelpComm()
    }
  },
  // 获取我的助力列表
  getHelpList () {
    apiGetHelpComplete().then(
      res => {
        this.setData({
          myHelp: res.data,
          loads: false
        })
        wx.stopPullDownRefresh()
      }
    )
  },
  // 获取助力订单列表
  getHelpOrder () {
    apiGetHelpUserOrderList().then(
      res => {
        this.setData({
          helpOrder: res.data.data,
          loads: false
        })
        wx.stopPullDownRefresh()
      }
    )
  },
  // 进入订单详情
  goToDetail (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../order-detail/order-detail?id=${id}&help_type=${1}`
    })
  },
  // 去支付
  toPayMoney (e) {
    wx.navigateTo({
      url: `../payment/payment?id=${e.target.dataset.id}&help_type=${1}`
    })
  },
  // 确认收货
  overGoods (e) {
    wx.showModal({
      title: '提示',
      content: '是否确认收货？',
      success: (res) => {
        if (res.confirm) {
          apiOrderTakeDelivery({ order_id: e.target.dataset.id }).then(
            res => {
              wx.showToast({
                title: '已收货',
                success: () => {
                  this.getHelpList()
                  this.getHelpOrder()
                  this.getHelpComm()
                }
              })
            }
          )
        }
      }
    })
  },
  // 查看物流
  logistics (e) {
    wx.navigateTo({
      url: `../logistics/logistics?id=${e.target.dataset.id}`
    })
  },
  // 获取助力红包列表
  getHelpComm () {
    apiGetHelpCommission().then(
      res => {
        for (let i = 0; i < res.data.length; i++) {
          res.data[i].create_time = formatTime(res.data[i].create_time, 'Y-M-D h:m:s')
        }
        this.setData({
          helpComm: res.data,
          loads: false
        })
        wx.stopPullDownRefresh()
      }
    )
  },
  // 查看助力详情
  toHelpDetail (e) {
    if (e.currentTarget.dataset.item.order_id > 0) {
      wx.navigateTo({
        url: `../order-detail/order-detail?id=${e.currentTarget.dataset.item.order_id}&help_type=${1}`
      })
    } else {
      wx.navigateTo({
        url: `../assistance-detail/assistance-detail?id=${e.currentTarget.dataset.item.id}&help_list=1`
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    this.setData({
      myHelp: [],
      helpOrder: [],
      helpComm: []
    })
    this.getHelpComm()
    this.getHelpList()
    this.getHelpOrder()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  }
})
